// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
var addButtonEventS =function (btn,seat,self,efunc) {

    btn.on(cc.Node.EventType.TOUCH_END, function(event){
        if (efunc) { efunc(btn,self,seat); }
    });

};
var DN_CMD = require("zjhCMD");
cc.Class({
    extends: cc.Component,

    properties: {
        className:"panellayer",
        minePanel:{
            default:null,
            type:cc.Prefab,
        },
        otherPanel:{
            default:null,
            type:cc.Prefab,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad: function () {
        this.node.thisJS = this;
        // this.user0panel =  cc.find("minepanel",this.node);
        // this.user1panel =  cc.find("user2panel",this.node);
        // this.user2panel =  cc.find("user3panel",this.node);
        // this.user3panel =  cc.find("user4panel",this.node);
        // this.user4panel =  cc.find("user5panel",this.node);
        // this.user5panel =  cc.find("user6panel",this.node);

        // for(i=0;i<6;i++){
        //     console.log("user"+i+"panel Pos--->");
        //     dump(this["user"+i+"panel"].getPosition());
        //     var list = this["user"+i+"panel"].getChildren();
        //    for(var k in list){
        //      console.log("user"+i+"--->"+list[k].getName());
        //      dump(list[k].getPosition());
        //    } 
        // }

        //动态添加刷新节点
        for(var i=0;i<GameData.PlayerNum;i++){
            var config = GameConfig.panelConfig[i]
            var prefab = this.minePanel ;
            if(i>0){
              prefab = this.otherPanel ;
            }
            var panelNode = cc.instantiate(prefab);
            this.node.addChild(panelNode);
            this["user"+i+"panel"] = panelNode ;
            if(i==0){
            addButtonEventS(panelNode,i,this,this.panelClick);
            }
            var script = panelNode.getComponent('MyPanelView');
            if(i>0){
              script = panelNode.getComponent('panelView');
            }
            panelNode.setPosition(config.pos);
            script.updateUI(config.subPos);
        }
       // this.test();
        this.initUI();
    },

    test:function(){
      for(var i=0;i<GameData.PlayerNum;i++){
        this["user"+i+"panel"].active = true ;
        this["user"+i+"panel"].thisJS.sendCard(); 
        this["user"+i+"panel"].thisJS.showEndCard({
          card:[1,2,3,4,5],
          card_type:1,
        })
      }
    },
    //点击响应
    /*
    // cmd: 1702  用户请求看牌  注意看牌不在重新给时间了
    message GoldFlowerUserLookCardReq{
      optional int32 uin = 1;     //用户ID
      optional int32 desk_id=2;     //桌子ID
    }
    */
    panelClick:function(view,self,seat){
      if(self.canKanPai && (self.isKanPai==false)){
        GameNet.sendRequest({cmd:DN_CMD.GoldFlowerUserLookCardReq,
            body:{
                uin: UserInfoModel.info.uin,
                desk_id:GameData.deskid,
        }});
      }
    },

    start: function () {
        
    },
    initUI: function(){
         console.log("接收监听 开始游戏");
        EventManager.addEventObeserveF(EventConfig.startGame, handler(this,
            this.gameStart),"panellayer");
        EventManager.addEventObeserveF(EventConfig.xiaZhu, handler(this,
            this.xiaZhuNtf),"panellayer");
        EventManager.addEventObeserveF(EventConfig.qiPai, handler(this,
            this.qiPaiNtf),"panellayer");
        EventManager.addEventObeserveF(EventConfig.clearTable, handler(this,
            this.clearTable),"panellayer");
        EventManager.addEventObeserveF(EventConfig.UserEnterDeskNtf, handler(this,
            this.UserEnterDeskNtf),"panellayer");
        EventManager.addEventObeserveF(EventConfig.kanPaiNtf, handler(this,
            this.kanPaiNtf),"panellayer");
        EventManager.addEventObeserveF(EventConfig.gameoverNtf, handler(this,
            this.gameoverNtf),"panellayer");
        EventManager.addEventObeserveF(EventConfig.compareNtf, handler(this,
            this.compareNtf),"panellayer");
        EventManager.addEventObeserveF(EventConfig.roundOverCompareCard, handler(this,
            this.roundOverCompareCard),"panellayer");
        this.canKanPai = false ;
        this.isKanPai = false ; //是否已经看了牌
    },
    
    //重连开牌
    UserEnterDeskNtf:function(data){
      // status  1 等待状态  2 抢庄状态  3 叫分状态  4 出牌状态  5 结束状态
      if(data.status == 1){
        console.log("重练发牌");
        var info = GameData ;
        for(var k in info.playerinfo){
            var user = info.playerinfo[k];
            var searId = GameData.getUserSeatP(user.seatid);
            console.log("user status"+user.status);
            if (user.status>GameConfig.playerStatus.USER_STATE_READY){
              this["user"+searId+"panel"].active = true ;
              this["user"+searId+"panel"].thisJS.sendCard(user,true); 
              if(user.status == GameConfig.playerStatus.USER_STATE_LOOK_CARD){
                this["user"+searId+"panel"].thisJS.showKanCard(user,true); //显示看牌
              }
              if(user.status == GameConfig.playerStatus.USER_STATE_GIVEUP){
                this["user"+searId+"panel"].thisJS.showEndCard(user,true); //显示看牌
              }
             // this["user"+searId+"panel"].thisJS.showEndCard(user,true); //弃牌
             // this["user"+searId+"panel"].thisJS.showKanCard(user,true); //显示看牌
             // this["user"+searId+"panel"].thisJS.showKanCard(user,true); //显示输牌
            }
         }
      }

    },
    /*
    弃牌
      message GoldFlowerUserFlodNtf{
      optional int32 uin = 1;         //用户ID
      optional int32 desk_id=2;         //桌子ID
      optional int32 round_count=3;     //当前轮数
      optional int32 max_count=4;       //最大轮数
      optional int64 user_money=5;      //用户当前下的钱
      optional int64 desk_total_chip=6;   //桌子当前总注数
      optional int32 next_uin=7;        //下一个操作的玩家  如果为-1 表示当局已经结束 等待game_over指令
      optional int32 can_operator = 8;    // 可以执行的操作 包括 看牌 比牌 弃牌  用位数表示  111  第一位看牌  第二位比牌 第三位弃牌  新加一位  第五位是代表目前是火拼状态
      repeated int32 card = 9;        //弃牌用户手牌
      optional int32 card_type = 10;      //弃牌手牌类型
    }
    */
    qiPaiNtf:function(data){
      console.log("qiPaiNtf");
      // if(data.uin == UserInfoModel.info.uin){
      //     var info = GameData ;
      //     dump(info.playerinfo);
      //     for(var k in info.playerinfo){
      //         if(info.playerinfo[k].uin==UserInfoModel.info.uin){
      //             var user = info.playerinfo[k];
      //             var searId = GameData.getUserSeatP(user.seatid);
      //             this["user"+searId+"panel"].active = true;
      //             this["user"+searId+"panel"].thisJS.showEndCard(user,true);
      //         }
      //     }
      // }else{
        var info = GameData ;
         for(var k in info.playerinfo){
              if(info.playerinfo[k].uin==data.uin){
                  var user = info.playerinfo[k];
                  var searId = GameData.getUserSeatP(user.seatid);
                  this["user"+searId+"panel"].active = true;
                  this["user"+searId+"panel"].thisJS.showEndCard(user,true);
              }
          }
        if(data.next_uin == UserInfoModel.info.uin){
          var isKP = Math.floor(data.can_operator /10);
          if(isKP==1){
            console.log("显示点击看牌");
            this.canKanPai = true ;
            if(this.isKanPai){
              this["user"+0+"panel"].thisJS.updateKanState(false);
            }else{
              this["user"+0+"panel"].thisJS.updateKanState(true);
            }
          }else{
            this.canKanPai = false ;
            this["user"+0+"panel"].thisJS.updateKanState(false);
          }
        }else{
          this.canKanPai = false ;
        }
      // }
    },
    kanPaiNtf:function(data){
         var info = GameData ;
         for(var k in info.playerinfo){
              if(info.playerinfo[k].uin==data.uin){
                  var user = info.playerinfo[k];
                  var searId = GameData.getUserSeatP(user.seatid);
                  this["user"+searId+"panel"].active = true;
                  this["user"+searId+"panel"].thisJS.showKanCard(user,true);
              }
          }
        if(data.uin == UserInfoModel.info.uin){
          this.isKanPai = true ;
          this["user"+0+"panel"].thisJS.updateKanState(false);
        }
    },
    /*
        -------------------dump start------------------
 38 {
 53 　　cmd: 1714
 53 　　ret: 0
 53 　　sn: 0
 44 　　model: 
 38 　　{
 53 　　　　winner: 3614
 44 　　　　result: 
 38 　　　　{
 44 　　　　　　0: 
 38 　　　　　　{
 53 　　　　　　　　uin: 3614
 53 　　　　　　　　win_money: 4
 53 　　　　　　　　showcard: 0
 44 　　　　　　　　card: 
 38 　　　　　　　　{
 53 　　　　　　　　　　0: 32
 53 　　　　　　　　　　1: 24
 53 　　　　　　　　　　2: 53
 56 　　　　　　　　}
 53 　　　　　　　　card_type: 0
 53 　　　　　　　　user_money: 8
 56 　　　　　　}
 44 　　　　　　1: 
 38 　　　　　　{
 53 　　　　　　　　uin: 3615
 53 　　　　　　　　win_money: -4
 53 　　　　　　　　showcard: 0
 44 　　　　　　　　card: 
 38 　　　　　　　　{
 53 　　　　　　　　　　0: 37
 53 　　　　　　　　　　1: 41
 53 　　　　　　　　　　2: 26
 56 　　　　　　　　}
 53 　　　　　　　　card_type: 0
 53 　　　　　　　　user_money: -4
 56 　　　　　　}
 56 　　　　}
 53 　　　　dealer_seatid: 3
 53 　　　　start_time: 8
 53 　　　　show_card_time: 5
 56 　　}
 56 }
    */
    gameoverNtf:function(data){
         var info = GameData ;
         for(var k in data.result){
              var userInfo = data.result[k];
              var user = GameData.playerinfo[userInfo.uin] ;
              var searId = GameData.getUserSeatP(user.seatid);
              this["user"+searId+"panel"].active = true;
              //显示牌
              if(userInfo.showcard==1){          
                this["user"+searId+"panel"].thisJS.showEndCard(user);    
              }
              if(data.winner != userInfo.uin){
                if(userInfo.showcard!=1){
                  this["user"+searId+"panel"].thisJS.showLoseCard(true);
                }else{
                  this["user"+searId+"panel"].thisJS.showLoseCard(false);
                }
              }
              if(data.winner == userInfo.uin){
                this["user"+searId+"panel"].thisJS.showFenImg(userInfo.card_type);
              }
              console.log("飘钱"+userInfo.win_money);

        }
        this.scheduleOnce(function(){
            for(var k in data.result){
                var userInfo = data.result[k];
                var user = GameData.playerinfo[userInfo.uin] ;
                var searId = GameData.getUserSeatP(user.seatid);
                if(userInfo.win_money)
                {    
                    console.log("飘钱"+userInfo.win_money);
                    this["user"+searId+"panel"].thisJS.winMoneyFly(userInfo.win_money);
                }
            }
        },1);

    },

    //比牌
    compareNtf:function(data){
      this.scheduleOnce(function(){
        var info = GameData ;
        for(var k in info.playerinfo){
            if(info.playerinfo[k].uin==data.uin){
                var user = info.playerinfo[k];
                var searId = GameData.getUserSeatP(user.seatid);
                if(data.result == 0){  
                  this["user"+searId+"panel"].thisJS.showLoseCard(true);
                }
            }
            if(info.playerinfo[k].uin==data.compare_uin){
                var user = info.playerinfo[k];
                var searId = GameData.getUserSeatP(user.seatid);
                if(data.result == 1){  
                  this["user"+searId+"panel"].thisJS.showLoseCard(true);
                }
            }
         }
      },3)
    },
    //比牌
    roundOverCompareCard:function(data){
       var result = 0 ;
       if (data.userlist[0] == data.win_uin){
            result = 1 ;
       }
       var info = {
        uin:data.userlist[0] ,
        compare_uin:data.userlist[1],
        result:result,
       }
       this.compareNtf(info);
    },
    xiaZhuNtf:function(data){
      if(data.next_uin == UserInfoModel.info.uin){
          var isKP = Math.floor(data.can_operator /10);
          if(isKP==1){
            this.canKanPai = true ;
            console.log("显示点击看牌");
            if(this.isKanPai){
              this["user"+0+"panel"].thisJS.updateKanState(false);
            }else{
              this["user"+0+"panel"].thisJS.updateKanState(true);
            }
            
          }else{
            this.canKanPai = false ;
            this["user"+0+"panel"].thisJS.updateKanState(false);
          }
      }else{
          this["user"+0+"panel"].thisJS.updateKanState(false);
          this.canKanPai = false ;
      }
    },
    //玩家进入桌子,更新个人信息  
    gameStart:function(data){
         Log.logd("发牌");
       // this.scheduleOnce(function(){
          var info = GameData ;
          global.Audio.playSFX("games/douniu/sound/effect/game_start.wav");
           for(var k in info.playerinfo){
                  var user = info.playerinfo[k];
                  if (user.status>GameConfig.playerStatus.USER_STATE_READY){
                  var searId = GameData.getUserSeatP(user.seatid);
                  console.log("searId->"+searId);
                  this["user"+searId+"panel"].active = true ;
                  this["user"+searId+"panel"].thisJS.sendCard(); 
                  }
           }
          if(data.next_uin == UserInfoModel.info.uin){
            this.canKanPai = true ;
          }else{
            this.canKanPai = false ;
          }
      //  },1);
    },
    //清桌
    clearTable:function(){
      for(var j=0;j<GameData.PlayerNum;j++){
        console.log("j-->"+j);
        if(this["user"+j+"panel"].active){
        this["user"+j+"panel"].thisJS.clearTable();
        }
      }
      this.canKanPai = false ;
      this.isKanPai = false ; //是否已经看了牌
    }
});
